package com.fz.us.admin.base.bean.search.rule;

import com.fz.us.admin.base.bean.search.SearchEnum;
import org.hibernate.type.Type;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - RuleSql
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-13 。
 * ============================================================================
 */
public class RuleSql extends Rule {

    private final String sql;
    private final Object[] values;
    private final Type[] types;

    private RuleSql(SearchEnum.RuleType ruleType, String sql, Object[] values, Type[] types) {
        super(ruleType);
        this.sql = sql;
        this.values = values;
        this.types = types;
    }

    public String getSql() {
        return sql;
    }

    public Object[] getValues() {
        return values;
    }

    public Type[] getTypes() {
        return types;
    }

    public static RuleSql sql(String sql) {
        return newBuilder().setRuleType(SearchEnum.RuleType.sql).setSql(sql).build();
    }

    public static RuleSql sql(String sql, Object[] values, Type[] types) {
        return newBuilder().setRuleType(SearchEnum.RuleType.sql).setSql(sql).setValues(values).setTypes(types).build();
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder extends Rule.Builder<Rule, Builder> {

        private String sql;
        private Object[] values;
        private Type[] types;

        @Override
        protected Builder getThis() {
            return this;
        }

        public Builder setSql(String sql) {
            this.sql = sql;
            return this;
        }

        public Builder setValues(Object... values) {
            this.values = values;
            return this;
        }

        public Builder setTypes(Type... types) {
            this.types = types;
            return this;
        }

        @Override
        public RuleSql build() {
            Assert.notNull(ruleType, "ruleType 不能为空");
            Assert.hasText(sql, "sql 不能为空");
            return new RuleSql(ruleType, sql, values, types);
        }

    }

}
